package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.UserColumn;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-12-10 22:32:50
 */
@Component
public class UserColumnSpecifications {

    public static Specification<UserColumn> queryList(UserColumn userColumn) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (userColumn.getUcId() != null) {
                predicates.add(builder.equal(root.get("ucId" ).as(Long.class), userColumn.getUcId()));
            }
            if (userColumn.getRoleId() != null) {
                predicates.add(builder.equal(root.get("roleId" ).as(Long.class), userColumn.getRoleId()));
            }
            if (userColumn.getColumnId() != null) {
                predicates.add(builder.equal(root.get("columnId" ).as(Long.class), userColumn.getColumnId()));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
